﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Base class for all list controls that allow the selection of multiple items.
	/// </summary>
	public abstract class BaseMultiValueListControl : BaseListControl, IMultiValueListControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public BaseMultiValueListControl()
		{
			_mpv = new ListControlMultiPartValue(this);
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Comma-separated list of the values of all selected items in this list control. Doesn't require Items to be populated.
		/// </summary>
		public override string Value
		{
			get
			{
				return _mpv.Value;
			}
			set
			{
				_mpv.Value = value;
			}
		}
		/// <summary>
		/// Gets the values for all of the selected items for this list control. Doesn't require Items to be populated.
		/// </summary>
		public List<string> Values
		{
			get
			{
				return _mpv.ValueList;
			}
		}
		/// <summary>
		/// Gets all of the selected items for this list control. Only available when items are initialized (ie., initial data binding or subsequent post back with Items property posted).
		/// </summary>
		public List<ListItem> SelectedItems
		{
			get
			{
				return _mpv.SelectedItems;
			}
		}
		/// <summary>
		/// Gets the indices of all of the selected items for this list control. 
		/// </summary>
		/// <remarks>
		/// Only available when items are initialized (ie., initial data binding or subsequent post back with Items property posted).
		/// </remarks>
		public List<int> SelectedIndices
		{
			get
			{
				return this._mpv.SelectedIndices;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		ListControlMultiPartValue _mpv;
		#endregion
		#region METHODS
		/// <summary>
		/// Renders an option list item.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		public override void RenderItemOption(System.Web.UI.HtmlTextWriter writer, ListItem item)
		{
			if (this.Values.Contains(item.Value))
				writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Selected, "selected");
			base.RenderItemOption(writer, item);
		}
		#endregion
		#endregion
	}
}
